package com.example.service.impl;

import com.example.mapper.LoginMapper;
import com.example.mapper.StudentMapper;
import com.example.pojo.LoginMsg;
import com.example.pojo.Student;
import com.example.service.StudentService;
import com.example.utils.PasswordSecurity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public LoginMsg updateInfo(Student student){

        try {
            //获取数据库中该用户名的学生信息,判断旧密码是否正确
            Student requireStudent= studentMapper.select( student.getUsername());
            if(!("".equals(student.getOldPassword()))&&PasswordSecurity.checkPassword(student.getOldPassword(),requireStudent.getPassword())){
                //旧密码正确,将新密码加密后更新数据库
                student.setNewPassword(PasswordSecurity.hashPassword(student.getNewPassword()));
                studentMapper.updateInfo(student);

                //2.查询修改后的学生信息返回
                LoginMsg loginMsg = studentMapper.selectUpdated(student);
                if (loginMsg != null) {
                    loginMsg.setRole(1);
                }
                return loginMsg;
            }else if("".equals(student.getOldPassword())){
                studentMapper.updateInfo(student);
                //2.查询修改后的学生信息返回
                LoginMsg loginMsg = studentMapper.selectUpdated(student);
                if (loginMsg != null) {
                    loginMsg.setRole(1);
                }
                return loginMsg;
            }else{
                LoginMsg loginMsg = new LoginMsg();
                loginMsg.setMsg("原密码错误");
                return loginMsg;
            }

        }catch (Exception e){
            StringBuilder msg = new StringBuilder(e.getMessage());
            if(msg.toString().contains("Duplicate entry")&&msg.toString().contains("for key 'students.username'")){
                msg.setLength(0);
                msg.append("该用户名已存在");
            }else if(msg.toString().contains("Duplicate entry")&&msg.toString().contains("for key 'students.email'")){
                msg.setLength(0);
                msg.append("该邮箱已存在,请检查是否填写错误");
            }else if(msg.toString().contains("too long")&&msg.toString().contains("'username'")){
                msg.setLength(0);
                msg.append("用户名长度不能超过20个字符");
            }else if(msg.toString().contains("too long")&&msg.toString().contains("'name'")) {
                msg.setLength(0);
                msg.append("姓名长度不能超过20个字符");
            }else if (msg.toString().contains("too long")&&msg.toString().contains("'password'")) {
                msg.setLength(0);
                msg.append("密码长度太长");
            }else{
                msg.setLength(0);
                msg.append("修改失败,请检查是否填写错误");
            }
            LoginMsg loginMsg= new LoginMsg();
            loginMsg.setMsg(msg.toString());
            return loginMsg;
        }
    }
}
